package com.pms.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import com.pms.entity.User;
import com.pms.entity.UserProject;
import com.pms.util.DBHelp;
import com.pms.util.RowMapper;

public class UserDao {
	private DBHelp<User> db=new DBHelp<User>();
	User user=new User();
	
	public void save(User user){
		String sql="insert into t_user(userid,username,`password`,nickname,picture) values(?,?,?,?,?)";
		db.executeSQL(sql, user.getUserId(),user.getUserName(),user.getPassword(),user.getNickName(),user.getPicture());
		
	}
	
	public User FindByName(String nickName){
		String sql="SELECT userid,username,`password`,nickname,picture FROM t_user where nickname=?";
		return db.executeQueryForObject(sql, new UserRowMapper(),nickName);
	}
	
	
	
	public List<User> findAllUser(){
		String sql="SELECT userid,username,`password`,nickname,picture FROM t_user";	
		return db.executeQueryForList(sql,new UserRowMapper());	
	}

	
	public List<User> findByProjectId(UserProject up) {
		String sql = "SELECT userid,username,password,nickname,picture FROM t_user WHERE userid IN(SELECT userid FROM t_user_project WHERE projectid = ?)";
		return db.executeQueryForList(sql, new UserRowMapper(), up.getProject().getProjectId());
	}
	
	
	public User findById(String manager) {
		String sql = "SELECT userid,username,`password`,nickname,picture FROM t_user WHERE userid = ?";
		return db.executeQueryForObject(sql, new UserRowMapper(), manager);
	}

	public class UserRowMapper implements RowMapper<User>{
		
		
		public User mapRow(ResultSet rs) throws SQLException {
			User user=new User();
			user.setUserId(rs.getString("userId"));
			user.setUserName(rs.getString("userName"));
			user.setPassword(rs.getString("password"));
			user.setNickName(rs.getString("nickName"));
			user.setPicture(rs.getString("picture"));
			return user;
		}
		
		
	}

	
	
}
